<?php
	namespace Common\Logic;
	use Common\Model\CustomOrdersModel;
	class CustomOrdersLogic extends CustomOrdersModel{
		/*
		* Function:		getCustomOrdersCateList
		* Description:	定制订单列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getCustomOrdersList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getCustomOrdersFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getCustomOrdersCateFilter
		* Description:	定制订单列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getCustomOrdersFilter($filter = ''){
			$conditon = array();
            $conditon['is_deleted'] = 0;
			if(!empty($filter['order_id'])){
				$conditon['order_no'] = $filter['order_id'];
			}
			if(!empty($filter['status'])){
				$conditon['status'] = $filter['status'];
			}
			if(!empty($filter['user_id'])){
				$conditon['user_id'] = $filter['user_id'];
			}
			if(!empty($filter['status'])){
				$conditon['status'] = $filter['status'];
			}
			if(!empty($filter['user_name'])){
				$wh = array();
				$wh['username'] = array('like','%'.$filter['user_name'].'%');
				$sql = D('User')->field('id')->where($wh)->select(false);
				$conditon['user_id'] = array('exp','in('.$sql.')');
			}
			if(!empty($filter['addtime_fromdate']) || !empty($filter['addtime_todate'])){
				if(!empty($filter['addtime_fromdate']) && !empty($filter['addtime_todate'])){
					$conditon['addtime'] = array(array('egt',$filter['addtime_fromdate']),array('elt',$filter['addtime_todate']));
				}else if(!empty($filter['addtime_fromdate'])){
					$conditon['addtime'] = array('egt',$filter['addtime_fromdate']);
				}else if(!empty($filter['addtime_todate'])){
					$conditon['addtime'] = array('elt',$filter['addtime_todate']);
				}
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getCustomOrdersCount
		* Description:	定制订单列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getCustomOrdersCount($filter = ''){
			$conditon = $this->getCustomOrdersFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addCustomOrders
		* Description:	增加定制订单
		* @param:		$data array 增加的数组
		*/
		public function addCustomOrders($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getCustomOrdersInfo
		* Description:	获取定制订单信息 by row
		* @param:		$id int ID
		*/
		public function getCustomOrdersInfo($id){
			return $this->where(array('id'=>$id))->find();
		}

        /*
		* Function:		getCustomOrdersInfo
		* Description:	获取定制订单信息 by row
		* @param:		$id int ID
		*/
		public function getCustomOrdersInfoByOrderId($order_id = ''){
			return $this->where(array('order_no'=>$order_id))->find();
		}
		
		/*
		* Function:		updateCustomOrders
		* Description:	更新定制订单
		* @param:		$data array 更新的数组
		*/
		public function updateCustomOrders($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteCustomOrders
		* Description:	删除定制订单
		* @param:		$id int ID
		*/
		public function deleteCustomOrders($id){
			return $this->where(array('id'=>$id))->delete();
		}
		
		/*
		* Function:		getCustomOrdersAllList
		* Description:	定制订单列表
		*/
		public function getCustomOrdersAllList($pid = 0){
			$conditon = [];
			$conditon['status'] = 1;
			$conditon['pid'] = $pid;
			return $this->where($conditon)->select();
		}

        /*
		* Function:		createCustomOrders
		* Description:	生成定制订单
		*/
		public function createCustomOrders($data = [],$dataList = []){
			$return = [];
			$data['order_no'] = $this->generateOrderId('DZ');
			$this->startTrans();
			$ret = $this->addCustomOrders($data);
			if(empty($ret)){
				$this->rollback();
				return ['code'=>0,'errorMsg'=>'生成定制订单失败'];
			}
			if(is_array($dataList)&&count($dataList)>0){
                if(count($dataList)>0){
                    foreach($dataList as $key=>$val){
                        $item = [];
                        $item['tid'] = $ret;
                        $item['cate_id'] = $val['id'];
                        if(!M('custom_order_list')->add($item)){
                            $this->rollback();
				            return ['code'=>0,'errorMsg'=>'生成定制订单明细失败'];
                        }
                    }
                }
			}else{
			    $this->rollback();
				return ['code'=>0,'errorMsg'=>'缺少订单明细参数'];
			}
			//增加商品库存冻结
			$this->commit();
			return ['code'=>1,'errorMsg'=>'success'];
		}

        /*
		* Function: generateOrderId
		* Description:生成唯一订单号
		* @param:$supplierCode string 商家编码orcode
		*/
		public function generateOrderId($code = ''){
			$order_id = $code.date('YmdHis',time()).rand(10000,99999);
			if($this->getCustomOrdersInfoByOrderId($order_id)){
				return $this->generateOrderId($code);
			}
			return $order_id;
		}
		
		/*
		* 获取订单状态
		*/
		public function getOrderStatusList(){
			return array(
				array('name'=>'进行中','value'=>'0'),
				array('name'=>'已完成','value'=>'1'),
				array('name'=>'已取消','value'=>'2')
			);
		}
	}
?>